## Warning: package 'BiocGenerics' was built under R version 4.0.5
## Warning: package 'GenomeInfoDb' was built under R version 4.0.5
load("~/Documents/MiGASti/Databases/gene_matrix.RData")
metadata <- read.table("~/Documents/MiGASti/Databases/metadata2.txt")
#set rownames to Sample
row.names(metadata) <- metadata$Sample 
setwd("~/Documents/MiGASti/Databases")
#exclude samples that did not pass QC filtering
exclude <- read.table("samples2remove2.txt")
exclude <- exclude$x
genes_counts_filt = genes_counts[, !colnames(genes_counts) %in% exclude] 
#Excludes the samples from filters. 
#dim(genes_counts_filt)
metadata_filt = metadata[ !(rownames(metadata) %in% exclude), ]
length(metadata_filt)
gencode_30 = read.table("~/Documents/MiGASti/Databases/ens.geneid.gencode.v30")
colnames(gencode_30) = c("ensembl","symbol")
#order metadata and genes counts
genes_counts_ordered <- genes_counts_filt[,rownames(metadata_filt)]
#head(genes_counts_ordered)
all(rownames(metadata_filt) == colnames (genes_counts_ordered)) #TRUE

Preparing the samples for DEG

#remove uncultured samples
metadata_cultured <- metadata_filt[metadata_filt$Stimulation != "ununstim",]
metadata_cultured_subset <- metadata_cultured[metadata_cultured$Stimulation != "TNFa",]
#check numbers
#dim(metadata_cultured)
#check numbers per stimulation
#table(metadata_filt$Stimulation)
#select only SVZ samples in metadata
metadata_SVZ = metadata_cultured[metadata_cultured$Region=="SVZ",]
#select only SVZ samples in genes counts
genes_counts_SVZ <- genes_counts_ordered[,metadata_SVZ$Sample]
#order metadata and genes_counts
genes_counts_SVZ_ordered <- genes_counts_SVZ[,rownames(metadata_SVZ)]
#check ordering
all(rownames(metadata_SVZ) == colnames (genes_counts_SVZ_ordered)) #TRUE

[1] TRUE

#round counts; deseq2 can only handle integers
genes_counts_SVZ_ordered <- round(genes_counts_SVZ_ordered, digits=0)

#make sure covariate variables are the right format 
#cannot create dds object with numeric values
metadata_SVZ$Donor_id <- as.factor(metadata_SVZ$Donor_id)
metadata_SVZ$age <- as.integer(metadata_SVZ$age)
metadata_SVZ$sex <- as.factor(metadata_SVZ$sex)
metadata_SVZ$Stimulation <- as.factor(metadata_SVZ$Stimulation)
metadata_SVZ$picard_pct_ribosomal_bases = scale(metadata_SVZ$picard_pct_ribosomal_bases)
metadata_SVZ$picard_pct_mrna_bases = scale(metadata_SVZ$picard_pct_mrna_bases)
metadata_SVZ$picard_pct_pf_reads_aligned = scale(metadata_SVZ$picard_pct_pf_reads_aligned)
metadata_SVZ$picard_pct_percent_duplication = scale(metadata_SVZ$picard_pct_percent_duplication)

#adjust for: ~ age + (1|donor_id) + picard_pct_ribosomal_bases + picard_pct_mrna_bases +   picard_pct_percent_duplication + picard_pct_pf_reads_aligned 
table(metadata_SVZ$Stimulation)

ATP IFNy LPS R848 TNFa unstim 1 24 29 24 23 29

DESeq2 of SVZ samples

#createDeSEQ2 object for LPS
dds <- DESeqDataSetFromMatrix(countData = genes_counts_SVZ_ordered,
                              colData = metadata_SVZ,
                              design = ~ age + sex + picard_pct_ribosomal_bases + picard_pct_mrna_bases + picard_pct_percent_duplication + picard_pct_pf_reads_aligned + Stimulation) 
#variable of interest at end of the formula

#Make sure that control group is set as the reference group
dds$Stimulation <- relevel(dds$Stimulation, ref="unstim")
#head(dds)

#filter: CPM > 1 in 50% of the samples 
keep.exp = rowSums(cpm(genes_counts_SVZ_ordered) > 1) >= 0.5*ncol(genes_counts_SVZ_ordered)
dds = dds[keep.exp,]

#Run differential expression 
dds <- DESeq(dds, betaPrior = FALSE)
resultsNames(dds)

[1] “Intercept” “age”
[3] “sex_m_vs_f” “picard_pct_ribosomal_bases”
[5] “picard_pct_mrna_bases” “picard_pct_percent_duplication” [7] “picard_pct_pf_reads_aligned” “Stimulation_ATP_vs_unstim”
[9] “Stimulation_IFNy_vs_unstim” “Stimulation_LPS_vs_unstim”
[11] “Stimulation_R848_vs_unstim” “Stimulation_TNFa_vs_unstim”

DESeq2: LPS vs unstim

Number of differentially expressed genes

# generate results table for LPS vs unstim
res_LPS <- results(dds, name="Stimulation_LPS_vs_unstim")
sum(res_LPS$padj < 0.05, na.rm=TRUE)

[1] 1895

resOrdered_LPS <- res_LPS[order(res_LPS$pvalue),] 
resOrdered_LPS <- as.data.frame(resOrdered_LPS)

Volcano plot LPS vs unstim

head(res_LPS)

log2 fold change (MLE): Stimulation LPS vs unstim Wald test p-value: Stimulation LPS vs unstim DataFrame with 6 rows and 6 columns baseMean log2FoldChange lfcSE stat pvalue ENSG00000000419.12 550.2241 -0.000335487 0.170890 -0.00196318 0.99843361 ENSG00000000457.14 135.0023 0.062960542 0.131908 0.47730751 0.63314316 ENSG00000000460.17 49.2311 -0.410878255 0.150004 -2.73912018 0.00616039 ENSG00000000938.13 831.7376 0.003498006 0.189284 0.01848023 0.98525575 ENSG00000000971.15 24.8957 0.216007896 0.251576 0.85861922 0.39055063 ENSG00000001036.13 891.9734 -0.378237379 0.151511 -2.49643773 0.01254477 padj ENSG00000000419.12 0.9991721 ENSG00000000457.14 0.8039685 ENSG00000000460.17 0.0516664 ENSG00000000938.13 0.9930682 ENSG00000000971.15 0.6266177 ENSG00000001036.13 0.0826827

with(res_LPS, plot(log2FoldChange, -log10(pvalue), pch=20, main="Volcano plot", xlim=c(-2.5,2)))
with(subset(res_LPS, padj<.05 ), points(log2FoldChange, -log10(pvalue), pch=20, col="red"))

MA plot LPS vs unstim

#The function plotMA shows the log2 fold changes attributable to a given variable over the mean of normalized counts for all the samples in the DESeqDataSet. Points will be colored if the adjusted p value is less than 0.1. Points which fall out of the window are plotted as open triangles pointing either up or down.

plotMA(res_LPS, ylim=c(-2,2))

### TOP differentially expressed genes LPS vs unstim

setDT(resOrdered_LPS, keep.rownames = "ensembl")
resOrdered_LPS <- left_join(resOrdered_LPS, gencode_30, by = "ensembl")
resOrdered_LPS_top = resOrdered_LPS[order(resOrdered_LPS$padj) ,]
setDT(resOrdered_LPS_top, keep.rownames = "ensembl")
resOrdered_LPS_top = resOrdered_LPS_top[, c("ensembl", "symbol", "baseMean", "log2FoldChange", "lfcSE", "stat", "pvalue", "padj")]
createDT(resOrdered_LPS_top)
write.table(resOrdered_LPS_top, "DEG_LPS_SVZ.txt")

DESeq2: IFNy vs unstim

Number of differentially expressed genes

# generate results table for IFNy vs unstim
res_IFNy <- results(dds, name="Stimulation_IFNy_vs_unstim")
sum(res_IFNy$padj < 0.05, na.rm=TRUE)

[1] 292

resOrdered_IFNy <- res_IFNy[order(res_IFNy$pvalue),] 
resOrdered_IFNy <- as.data.frame(resOrdered_IFNy)

Volcano plot IFNy vs unstim

head(res_IFNy)

log2 fold change (MLE): Stimulation IFNy vs unstim Wald test p-value: Stimulation IFNy vs unstim DataFrame with 6 rows and 6 columns baseMean log2FoldChange lfcSE stat pvalue ENSG00000000419.12 550.2241 0.0919456 0.181438 0.506760 6.12323e-01 ENSG00000000457.14 135.0023 0.6299151 0.139473 4.516379 6.29060e-06 ENSG00000000460.17 49.2311 -0.0935664 0.158802 -0.589200 5.55727e-01 ENSG00000000938.13 831.7376 0.0268278 0.200489 0.133812 8.93551e-01 ENSG00000000971.15 24.8957 0.4895726 0.265929 1.840989 6.56231e-02 ENSG00000001036.13 891.9734 -0.1705949 0.160588 -1.062313 2.88094e-01 padj ENSG00000000419.12 0.937047192 ENSG00000000457.14 0.000628309 ENSG00000000460.17 0.915888952 ENSG00000000938.13 0.983258592 ENSG00000000971.15 0.624398269 ENSG00000001036.13 0.818162133

with(res_IFNy, plot(log2FoldChange, -log10(pvalue), pch=20, main="Volcano plot", xlim=c(-10,10)))
with(subset(res_IFNy, padj<.05 ), points(log2FoldChange, -log10(pvalue), pch=20, col="red"))

MA plot IFNy vs unstim

plotMA(res_IFNy, ylim=c(-2,2))

TOP differentially expressed genes IFNy vs unstim

setDT(resOrdered_IFNy, keep.rownames = "ensembl")
resOrdered_IFNy <- merge(resOrdered_IFNy, gencode_30, by = "ensembl")
resOrdered_IFNy_top = resOrdered_IFNy[order(resOrdered_IFNy$padj) ,]
setDT(resOrdered_IFNy_top, keep.rownames = "ensembl")
resOrdered_IFNy_top = resOrdered_IFNy_top[, c("ensembl", "symbol", "baseMean", "log2FoldChange", "lfcSE", "stat", "pvalue", "padj")]
createDT(resOrdered_IFNy_top)
write.table(resOrdered_IFNy_top, "DEG_IFNy_SVZ.txt")

DESeq2: R848 vs unstim

Number of differentially expressed genes

# generate results table for R848 vs unstim
res_R848 <- results(dds, name="Stimulation_R848_vs_unstim")
sum(res_R848$padj < 0.05, na.rm=TRUE)

[1] 708

resOrdered_R848 <- res_R848[order(res_R848$pvalue),] 
resOrdered_R848 <- as.data.frame(resOrdered_R848)

Volcano plot R848 vs unstim

head(res_R848)

log2 fold change (MLE): Stimulation R848 vs unstim Wald test p-value: Stimulation R848 vs unstim DataFrame with 6 rows and 6 columns baseMean log2FoldChange lfcSE stat pvalue ENSG00000000419.12 550.2241 0.271156 0.178618 1.518073 0.128996 ENSG00000000457.14 135.0023 0.110233 0.138090 0.798268 0.424715 ENSG00000000460.17 49.2311 -0.232806 0.156565 -1.486955 0.137027 ENSG00000000938.13 831.7376 0.192980 0.198033 0.974487 0.329815 ENSG00000000971.15 24.8957 0.233692 0.261628 0.893220 0.371739 ENSG00000001036.13 891.9734 -0.337990 0.158500 -2.132424 0.032972 padj ENSG00000000419.12 0.435828 ENSG00000000457.14 0.727897 ENSG00000000460.17 0.447037 ENSG00000000938.13 0.653632 ENSG00000000971.15 0.686369 ENSG00000001036.13 0.221824

with(res_R848, plot(log2FoldChange, -log10(pvalue), pch=20, main="Volcano plot", xlim=c(-10,10)))
with(subset(res_R848, padj<.05 ), points(log2FoldChange, -log10(pvalue), pch=20, col="red"))

MA plot R848 vs unstim

plotMA(res_R848, ylim=c(-2,2))

TOP differentially expressed genes R848 vs unstim

setDT(resOrdered_R848, keep.rownames = "ensembl")
resOrdered_R848 <- left_join(resOrdered_R848, gencode_30, by = "ensembl")
resOrdered_R848_top = resOrdered_R848[order(resOrdered_R848$padj) ,]
setDT(resOrdered_R848_top, keep.rownames = "ensembl")
resOrdered_R848_top = resOrdered_R848_top[, c("ensembl", "symbol", "baseMean", "log2FoldChange", "lfcSE", "stat", "pvalue", "padj")]
createDT(resOrdered_R848_top)
write.table(resOrdered_R848_top, "DEG_R848_SVZ.txt")

DESeq2: ATP vs unstim

#only one sample present, so removed this stimulation for further analysis. ### Number of differentially expressed genes

# generate results table for ATP vs unstim
res_ATP <- results(dds, name="Stimulation_ATP_vs_unstim")
sum(res_ATP$padj < 0.05, na.rm=TRUE)

[1] 0

resOrdered_ATP <- res_ATP[order(res_ATP$pvalue),] 
resOrdered_ATP <- as.data.frame(resOrdered_ATP)

Volcano plot ATP vs unstim

head(res_ATP)

log2 fold change (MLE): Stimulation ATP vs unstim Wald test p-value: Stimulation ATP vs unstim DataFrame with 6 rows and 6 columns baseMean log2FoldChange lfcSE stat pvalue ENSG00000000419.12 550.2241 0.8629612 0.674777 1.278884 0.200938 ENSG00000000457.14 135.0023 -0.0923940 0.538612 -0.171541 0.863798 ENSG00000000460.17 49.2311 -0.6161389 0.669877 -0.919778 0.357689 ENSG00000000938.13 831.7376 0.1391000 0.746344 0.186375 0.852151 ENSG00000000971.15 24.8957 0.6716788 0.912998 0.735685 0.461923 ENSG00000001036.13 891.9734 -0.0634169 0.601651 -0.105405 0.916055 padj ENSG00000000419.12 0.999939 ENSG00000000457.14 0.999939 ENSG00000000460.17 0.999939 ENSG00000000938.13 0.999939 ENSG00000000971.15 0.999939 ENSG00000001036.13 0.999939

with(res_ATP, plot(log2FoldChange, -log10(pvalue), pch=20, main="Volcano plot", xlim=c(-10,10)))
with(subset(res_ATP, padj<.05 ), points(log2FoldChange, -log10(pvalue), pch=20, col="red"))

MA plot ATP vs unstim

plotMA(res_ATP, ylim=c(-2,2))

TOP differentially expressed genes ATP vs unstim

setDT(resOrdered_ATP, keep.rownames = "ensembl")
resOrdered_ATP <- left_join(resOrdered_ATP, gencode_30, by = "ensembl")
resOrdered_ATP_top = resOrdered_ATP[order(resOrdered_ATP$padj) ,]
setDT(resOrdered_ATP_top, keep.rownames = "ensembl")
resOrdered_ATP_top = resOrdered_ATP_top[, c("ensembl", "symbol", "baseMean", "log2FoldChange", "lfcSE", "stat", "pvalue", "padj")]
createDT(resOrdered_ATP_top)
write.table(resOrdered_ATP_top, "DEG_ATP_SVZ.txt")

#Create genelists with Log2FC < 1 and < -1 for different stimuli

resOrdered_LPS_p <- subset(resOrdered_LPS_top, padj < 0.05)
resOrdered_LPS_LFC <- subset(resOrdered_LPS_p, log2FoldChange > 1 | log2FoldChange < -1)
write.table(resOrdered_LPS_LFC, "LPS_SVZ_FC1.txt")

resOrdered_IFNy_p <- subset(resOrdered_IFNy_top, padj < 0.05)
resOrdered_IFNy_LFC <- subset(resOrdered_IFNy_p, log2FoldChange > 1 | log2FoldChange < -1)
write.table(resOrdered_IFNy_LFC, "IFNy_SVZ_FC1.txt")

resOrdered_R848_p <- subset(resOrdered_R848_top, padj < 0.05)
resOrdered_R848_LFC <- subset(resOrdered_R848_p, log2FoldChange > 1 | log2FoldChange < -1)
write.table(resOrdered_R848_LFC, "R848_SVZ_FC1.txt")